package com.lmxdawn.api.common.api;

import com.lmxdawn.api.common.exception.ApiException;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Objects;

/**
 * API 业务断言
 * <p>参考：org.junit.Assert</p>
 *
 * @author jy
 * @since 2023-12-08 13:18
 */
public class ApiAssert {

    protected ApiAssert() {
        // to do noting
    }

    /**
     * 失败结果
     *
     * @param errorCode 异常错误码
     */
    public static void fail(IErrorCode errorCode) {
        throw new ApiException(errorCode);
    }

    /**
     * 失败结果
     *
     * @param condition 条件
     * @param errorCode 异常错误码
     */
    public static void fail(boolean condition, IErrorCode errorCode) {
        if (condition) {
            fail(errorCode);
        }
    }

    public static void isNull(Object obj, IErrorCode errorCode) {
        fail(Objects.isNull(obj), errorCode);
    }

    public static void nonNull(Object obj, IErrorCode errorCode) {
        fail(Objects.nonNull(obj), errorCode);
    }

    public static void isEmpty(Object obj, IErrorCode errorCode) {
        fail(ObjectUtils.isEmpty(obj), errorCode);
    }

    public static void nonEmpty(Object obj, IErrorCode errorCode) {
        fail(ObjectUtils.isNotEmpty(obj), errorCode);
    }

    public static void equals(Object a, Object b, IErrorCode errorCode) {
        fail(Objects.equals(a, b), errorCode);
    }

    public static void nonEquals(Object a, Object b, IErrorCode errorCode) {
        fail(!Objects.equals(a, b), errorCode);
    }

}
